var url = require('url');
var template = require('templateConfig');
var main = require('main');
var form = require('form');
var module = require('module');

//模板配置
var tplArr = [];
var modalTpl =require('raw!../template/modal.txt');
var orderTpl =require('raw!../template/order-detail.txt'); 
var moduleTpl =require('raw!../template/module.txt'); 
tplArr['module'] = template.compile(moduleTpl.replace(/^\s*|\s*$/g, "")); 
tplArr['modal'] = template.compile(modalTpl.replace(/^\s*|\s*$/g, "")); 
tplArr['order'] = template.compile(orderTpl.replace(/^\s*|\s*$/g, "")); 
 
var order_id = main.GetQueryString("order_id");
// var order_id = "1044";  
function orderDetail(){
    _this = this;
    this.main = function(){
        main.event(this,'tap','i-click'); 
        this.init();
    }    
    this.getFooter =function(type) {
        var data= {};
        if(!type || type=="order_detail") {
            data = {
                tType: "footer",
                lbtn: "手动发货",
                rbtn: "扫码发货",
                lclick: "byHead",
                rclick: "saoMa",
            }
            $(".wrap").append(tplArr['order'](data));
        }
        else if(type== "process"){
            data = {
                tType: "footer",
                active: "1",
                dataList: [{
                    link: url.Index,
                    icon: "ui-icon-home",
                    name: "首页"
                },{
                    link: url.shopCar,
                    icon: "ui-icon-cart",
                    name: "购物车",
                },{
                    link: url.Center,
                    icon: "ui-icon-personal",
                    name: "个人中心"
                }]
            }
            $(".wrap").append(module.getModule(data, "footer")) 
        }
        else if(type=="refund_detail"){
            data = {
                tType: "footer",
                lbtn: "同意退货",
                rbtn: "驳回退货",
                lclick: "",
                rclick: "",
            }
            $(".wrap").append(tplArr['order'](data));
        }
    }
    this.getContent = function(type){
        main.get(url.orderdetail+'?order_id='+order_id,{},function(ret){
        console.log(ret);
        var res = ret.data.data;
        var orderInfo = {
            status: res.order_status,
            number: res.order_sn,
            ordertime: res.add_time,
            // express:"顺丰快递",
            // expnumber:"SF683793",
            money: res.goods_amount,
            address: res.address,
            person: res.name,
            phone: res.tel,
            method :res.shipping_name,
            time : res.shipping_time,
            invoice: res.inv_title,
        }
        var goodsInfo = [];
        for(var n in res.order_goods){
             var good = res.order_goods[n];
             var _data = {
                    "id": 1,
                    "title":good.good_name,
                    // "attr": [],
                    "num":good.good_sku,
                    "spec":good.color+","+good.cupou+","+good.size,
                    "quantity":good.good_number,
                    "price":good.market_price,
                    // 2233222
                    "image":good.small_image
                };
            goodsInfo.push(_data);
        }
        
        var data = {
            tType: 'content',
            total: res.goods_amount,
            discount: res.coupons,
            money: res.order_amount,
            good_sku:res.order_goods[0].good_sku,
        }
        if(!type || type=="order_detail"){
            data.username= res.order_name;
            data.yongjin= res.money;
        }
        else if(type=="process"){
            orderInfo = {
                status: res.order_status,
                number: res.order_sn,
                ordertime: res.add_time,
                buttons: true,
                link1: url.goEvaluate+"?order_id="+res.order_id+"&good_sku="+data.good_num,
                name1: "去评价",
                link2: "",
                name2: "再次购买",
                money: res.goods_amount,
                address: res.address,
                person: res.name,
                phone: res.tel,
                method :res.shipping_name,
                time : res.shipping_time,
                invoice: res.inv_title,
            };
            // data.button= true;
            // data.btnname= "申请退换货";
            // data.link= "";
        }
        // 粉丝订单详情
        else if(type=='fans_detail'){
            orderInfo = {
                status: "已签收",
                number: "20389948",
                ordertime: "2016-08-12", 
                money: "99.00",
                address: "shanghai",
                person: "zhangsan",
                method: "send by store",
                time: "2016-09-01",
            };
            data.username= "Jessie Yang";
            data.yongjin= "4.95";
            data.back= true;
        }
        else if(type=='refund_detail'){
            data.username= "Jessie Yang";
            data.yongjin= "4.95";
            data.reason= "质量问题，尺寸偏小";
        }
 
        data.goods= module.ordersGoodsInfo(goodsInfo,"ordersGoodsInfo"),
        data.info= module.getModule(orderInfo, "orderinfo");
        $(".content").html(tplArr['order'](data)) 
        })
    }
    // 手动发货弹出层
    this.byHead = function() {
        main.get(url.orderdetail+'?order_id='+order_id,{},function(ret){
            var res = ret.data;
            var data = {
                tType: "showBotActionsheet",
                postUrl:url.ordersend,
                exp:res.express,
                order_id:res.data.order_id,
                click:"faHuo",
                pay_status:res.pay_status,
                shipping_status:res.shipping_status,
            }
            console.log(order_id);
            main.botActionsheet({"content":tplArr['module'](data)});
        })
    }
    // this.viewReady = function(){
        
    // }
    //扫码发货
    this.saoMa = function(){
        this.byHead();
        wx.ready(function(){
            wx.scanQRCode({
                needResult: 1,
                scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success: function (res) {
                    //  var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                    $("#head_btn").click();
                    var resultstr = res.resultStr;
                    var result = resultstr.split(",");
                    $("input[name='shipping_number']").val(result[1]);
                    //alert(result[1]);return;
                    //var id=$("#order_id").val();
                   // location.href="/index.php/admin/Ceshi/sel?order_id="+id+"&result="+result;
                    //return;
                }
            });
        })
    }
        
        
    // 手动发货
    this.faHuo = function(ths){
        if(ths.attr('isSubmit') == 'false'){return false;}
        form.ajaxSubmit(".orderForm",_this,ths);
    }
    this.formCallback= function(ret){
        if(ret.code== 1){
            window.location.href= "order-manager";
        }
    }
    this.init= function(){ 
         //this.getHead();
         if(typeof(_page)=="undefined")
            this.page= "";
         else 
            this.page= _page;
         this.getFooter(this.page);
         this.getContent(this.page);
         //this.viewReady();     
    }
    return this.main();
};

var orderdetail = new orderDetail();